{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MICCAI 2015 Abdominal dataset\n",
    "\n",
    "### Overview\n",
    "\n",
    "This is the second step of preprocessing\n",
    "\n",
    "Cut out irrelevant empty boundary and resample to 256-by-256 in axial plane.\n",
    "\n",
    "Input: intensity-normalized images\n",
    "\n",
    "Output: spacially resampled images\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n",
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "%reset\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import os\n",
    "import glob\n",
    "import SimpleITK as sitk\n",
    "import sys\n",
    "\n",
    "sys.path.insert(0, '../../dataloaders/')\n",
    "import niftiio as nio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "IMG_FOLDER = \"./tmp_normalized/\"\n",
    "SEG_FOLDER = IMG_FOLDER\n",
    "imgs = glob.glob(IMG_FOLDER + \"/image_*.nii.gz\")\n",
    "imgs = [ fid for fid in sorted(imgs) ]\n",
    "segs = [ fid for fid in sorted(glob.glob(SEG_FOLDER + \"/label_*.nii.gz\")) ]\n",
    "\n",
    "pids = [pid.split(\"_\")[-1].split(\".\")[0] for pid in imgs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "# helper functions copy pasted\n",
    "def resample_by_res(mov_img_obj, new_spacing, interpolator = sitk.sitkLinear, logging = True):\n",
    "    resample = sitk.ResampleImageFilter()\n",
    "    resample.SetInterpolator(interpolator)\n",
    "    resample.SetOutputDirection(mov_img_obj.GetDirection())\n",
    "    resample.SetOutputOrigin(mov_img_obj.GetOrigin())\n",
    "    mov_spacing = mov_img_obj.GetSpacing()\n",
    "\n",
    "    resample.SetOutputSpacing(new_spacing)\n",
    "    RES_COE = np.array(mov_spacing) * 1.0 / np.array(new_spacing)\n",
    "    new_size = np.array(mov_img_obj.GetSize()) *  RES_COE \n",
    "\n",
    "    resample.SetSize( [int(sz+1) for sz in new_size] )\n",
    "    if logging:\n",
    "        print(\"Spacing: {} -> {}\".format(mov_spacing, new_spacing))\n",
    "        print(\"Size {} -> {}\".format( mov_img_obj.GetSize(), new_size ))\n",
    "\n",
    "    return resample.Execute(mov_img_obj)\n",
    "\n",
    "def resample_lb_by_res(mov_lb_obj, new_spacing, interpolator = sitk.sitkLinear, ref_img = None, logging = True):\n",
    "    src_mat = sitk.GetArrayFromImage(mov_lb_obj)\n",
    "    lbvs = np.unique(src_mat)\n",
    "    if logging:\n",
    "        print(\"Label values: {}\".format(lbvs))\n",
    "    for idx, lbv in enumerate(lbvs):\n",
    "        _src_curr_mat = np.float32(src_mat == lbv) \n",
    "        _src_curr_obj = sitk.GetImageFromArray(_src_curr_mat)\n",
    "        _src_curr_obj.CopyInformation(mov_lb_obj)\n",
    "        _tar_curr_obj = resample_by_res( _src_curr_obj, new_spacing, interpolator, logging )\n",
    "        _tar_curr_mat = np.rint(sitk.GetArrayFromImage(_tar_curr_obj)) * lbv\n",
    "        if idx == 0:\n",
    "            out_vol = _tar_curr_mat\n",
    "        else:\n",
    "            out_vol[_tar_curr_mat == lbv] = lbv\n",
    "    out_obj = sitk.GetImageFromArray(out_vol)\n",
    "    out_obj.SetSpacing( _tar_curr_obj.GetSpacing() )\n",
    "    if ref_img != None:\n",
    "        out_obj.CopyInformation(ref_img)\n",
    "    return out_obj\n",
    "        \n",
    "## Then crop ROI\n",
    "def get_label_center(label):\n",
    "    nnz = np.sum(label > 1e-5)\n",
    "    return np.int32(np.rint(np.sum(np.nonzero(label), axis = 1) * 1.0 / nnz))\n",
    "\n",
    "def image_crop(ori_vol, crop_size, referece_ctr_idx, padval = 0., only_2d = True):\n",
    "    \"\"\" crop a 3d matrix given the index of the new volume on the original volume\n",
    "        Args:\n",
    "            refernce_ctr_idx: the center of the new volume on the original volume (in indices)\n",
    "            only_2d: only do cropping on first two dimensions\n",
    "    \"\"\"\n",
    "    _expand_cropsize = [x + 1 for x in crop_size] # to deal with boundary case\n",
    "    if only_2d:\n",
    "        assert len(crop_size) == 2, \"Actual len {}\".format(len(crop_size))\n",
    "        assert len(referece_ctr_idx) == 2, \"Actual len {}\".format(len(referece_ctr_idx))\n",
    "        _expand_cropsize.append(ori_vol.shape[-1])\n",
    "        \n",
    "    image_patch = np.ones(tuple(_expand_cropsize)) * padval\n",
    "\n",
    "    half_size = tuple( [int(x * 1.0 / 2) for x in _expand_cropsize] )\n",
    "    _min_idx = [0,0,0]\n",
    "    _max_idx = list(ori_vol.shape)\n",
    "\n",
    "    # bias of actual cropped size to the beginning and the end of this volume\n",
    "    _bias_start = [0,0,0]\n",
    "    _bias_end = [0,0,0]\n",
    "\n",
    "    for dim,hsize in enumerate(half_size):\n",
    "        if dim == 2 and only_2d:\n",
    "            break\n",
    "\n",
    "        _bias_start[dim] = np.min([hsize, referece_ctr_idx[dim]])\n",
    "        _bias_end[dim] = np.min([hsize, ori_vol.shape[dim] - referece_ctr_idx[dim]])\n",
    "\n",
    "        _min_idx[dim] = referece_ctr_idx[dim] - _bias_start[dim]\n",
    "        _max_idx[dim] = referece_ctr_idx[dim] + _bias_end[dim]\n",
    "        \n",
    "    if only_2d:\n",
    "        image_patch[ half_size[0] - _bias_start[0]: half_size[0] +_bias_end[0], \\\n",
    "                half_size[1] - _bias_start[1]: half_size[1] +_bias_end[1], ... ] = \\\n",
    "                ori_vol[ referece_ctr_idx[0] - _bias_start[0]: referece_ctr_idx[0] +_bias_end[0], \\\n",
    "                referece_ctr_idx[1] - _bias_start[1]: referece_ctr_idx[1] +_bias_end[1], ... ]\n",
    "\n",
    "        image_patch = image_patch[ 0: crop_size[0], 0: crop_size[1], : ]\n",
    "    # then goes back to original volume\n",
    "    else:\n",
    "        image_patch[ half_size[0] - _bias_start[0]: half_size[0] +_bias_end[0], \\\n",
    "                half_size[1] - _bias_start[1]: half_size[1] +_bias_end[1], \\\n",
    "                half_size[2] - _bias_start[2]: half_size[2] +_bias_end[2] ] = \\\n",
    "                ori_vol[ referece_ctr_idx[0] - _bias_start[0]: referece_ctr_idx[0] +_bias_end[0], \\\n",
    "                referece_ctr_idx[1] - _bias_start[1]: referece_ctr_idx[1] +_bias_end[1], \\\n",
    "                referece_ctr_idx[2] - _bias_start[2]: referece_ctr_idx[2] +_bias_end[2] ]\n",
    "\n",
    "        image_patch = image_patch[ 0: crop_size[0], 0: crop_size[1], 0: crop_size[2] ]\n",
    "    return image_patch\n",
    "\n",
    "   \n",
    "    \n",
    "def copy_spacing_ori(src, dst):\n",
    "    dst.SetSpacing(src.GetSpacing())\n",
    "    dst.SetOrigin(src.GetOrigin())\n",
    "    dst.SetDirection(src.GetDirection())\n",
    "    return dst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./tmp_normalized/image_0.nii.gz',\n",
       " './tmp_normalized/image_1.nii.gz',\n",
       " './tmp_normalized/image_10.nii.gz',\n",
       " './tmp_normalized/image_11.nii.gz',\n",
       " './tmp_normalized/image_12.nii.gz',\n",
       " './tmp_normalized/image_13.nii.gz',\n",
       " './tmp_normalized/image_14.nii.gz',\n",
       " './tmp_normalized/image_15.nii.gz',\n",
       " './tmp_normalized/image_16.nii.gz',\n",
       " './tmp_normalized/image_17.nii.gz',\n",
       " './tmp_normalized/image_18.nii.gz',\n",
       " './tmp_normalized/image_19.nii.gz',\n",
       " './tmp_normalized/image_2.nii.gz',\n",
       " './tmp_normalized/image_20.nii.gz',\n",
       " './tmp_normalized/image_21.nii.gz',\n",
       " './tmp_normalized/image_22.nii.gz',\n",
       " './tmp_normalized/image_23.nii.gz',\n",
       " './tmp_normalized/image_24.nii.gz',\n",
       " './tmp_normalized/image_25.nii.gz',\n",
       " './tmp_normalized/image_26.nii.gz',\n",
       " './tmp_normalized/image_27.nii.gz',\n",
       " './tmp_normalized/image_28.nii.gz',\n",
       " './tmp_normalized/image_29.nii.gz',\n",
       " './tmp_normalized/image_3.nii.gz',\n",
       " './tmp_normalized/image_4.nii.gz',\n",
       " './tmp_normalized/image_5.nii.gz',\n",
       " './tmp_normalized/image_6.nii.gz',\n",
       " './tmp_normalized/image_7.nii.gz',\n",
       " './tmp_normalized/image_8.nii.gz',\n",
       " './tmp_normalized/image_9.nii.gz']"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./tmp_normalized/label_0.nii.gz',\n",
       " './tmp_normalized/label_1.nii.gz',\n",
       " './tmp_normalized/label_10.nii.gz',\n",
       " './tmp_normalized/label_11.nii.gz',\n",
       " './tmp_normalized/label_12.nii.gz',\n",
       " './tmp_normalized/label_13.nii.gz',\n",
       " './tmp_normalized/label_14.nii.gz',\n",
       " './tmp_normalized/label_15.nii.gz',\n",
       " './tmp_normalized/label_16.nii.gz',\n",
       " './tmp_normalized/label_17.nii.gz',\n",
       " './tmp_normalized/label_18.nii.gz',\n",
       " './tmp_normalized/label_19.nii.gz',\n",
       " './tmp_normalized/label_2.nii.gz',\n",
       " './tmp_normalized/label_20.nii.gz',\n",
       " './tmp_normalized/label_21.nii.gz',\n",
       " './tmp_normalized/label_22.nii.gz',\n",
       " './tmp_normalized/label_23.nii.gz',\n",
       " './tmp_normalized/label_24.nii.gz',\n",
       " './tmp_normalized/label_25.nii.gz',\n",
       " './tmp_normalized/label_26.nii.gz',\n",
       " './tmp_normalized/label_27.nii.gz',\n",
       " './tmp_normalized/label_28.nii.gz',\n",
       " './tmp_normalized/label_29.nii.gz',\n",
       " './tmp_normalized/label_3.nii.gz',\n",
       " './tmp_normalized/label_4.nii.gz',\n",
       " './tmp_normalized/label_5.nii.gz',\n",
       " './tmp_normalized/label_6.nii.gz',\n",
       " './tmp_normalized/label_7.nii.gz',\n",
       " './tmp_normalized/label_8.nii.gz',\n",
       " './tmp_normalized/label_9.nii.gz']"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "segs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "Spacing: (0.66796875, 0.66796875, 3.0) -> [1.1689453125, 1.1689453125, 3.0]\n",
      "Size (448, 448, 147) -> [256. 256. 147.]\n",
      "./sabs_CT_normalizedv2/image_0.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_0.nii.gz has been saved\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "Spacing: (0.720703125, 0.720703125, 3.0) -> [1.26123046875, 1.26123046875, 3.0]\n",
      "Size (448, 448, 139) -> [256. 256. 139.]\n",
      "./sabs_CT_normalizedv2/image_1.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_1.nii.gz has been saved\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "Spacing: (0.68359375, 0.68359375, 3.0) -> [1.1962890625, 1.1962890625, 3.0]\n",
      "Size (448, 448, 143) -> [256. 256. 143.]\n",
      "./sabs_CT_normalizedv2/image_10.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_10.nii.gz has been saved\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.7675780057907104, 0.7675780057907104, 5.0) -> [1.3432615101337433, 1.3432615101337433, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "./sabs_CT_normalizedv2/image_11.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_11.nii.gz has been saved\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Label values: [ 0  1  2  3  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "Spacing: (0.70703125, 0.70703125, 5.0) -> [1.2373046875, 1.2373046875, 5.0]\n",
      "Size (448, 448, 96) -> [256. 256.  96.]\n",
      "./sabs_CT_normalizedv2/image_12.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_12.nii.gz has been saved\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "Spacing: (0.685546875, 0.685546875, 3.0) -> [1.19970703125, 1.19970703125, 3.0]\n",
      "Size (448, 448, 124) -> [256. 256. 124.]\n",
      "./sabs_CT_normalizedv2/image_13.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_13.nii.gz has been saved\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "Spacing: (0.83203125, 0.83203125, 5.0) -> [1.4560546875, 1.4560546875, 5.0]\n",
      "Size (448, 448, 85) -> [256. 256.  85.]\n",
      "./sabs_CT_normalizedv2/image_14.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_14.nii.gz has been saved\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "Spacing: (0.7792969942092896, 0.7792969942092896, 5.0) -> [1.3637697398662567, 1.3637697398662567, 5.0]\n",
      "Size (448, 448, 131) -> [256. 256. 131.]\n",
      "./sabs_CT_normalizedv2/image_15.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_15.nii.gz has been saved\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "Spacing: (0.7753909826278687, 0.7753909826278687, 5.0) -> [1.3569342195987701, 1.3569342195987701, 5.0]\n",
      "Size (448, 448, 88) -> [256. 256.  88.]\n",
      "./sabs_CT_normalizedv2/image_16.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_16.nii.gz has been saved\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "Spacing: (0.796875, 0.796875, 5.0) -> [1.39453125, 1.39453125, 5.0]\n",
      "Size (448, 448, 89) -> [256. 256.  89.]\n",
      "./sabs_CT_normalizedv2/image_17.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_17.nii.gz has been saved\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "Spacing: (0.875, 0.875, 3.0) -> [1.53125, 1.53125, 3.0]\n",
      "Size (448, 448, 100) -> [256. 256. 100.]\n",
      "./sabs_CT_normalizedv2/image_18.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_18.nii.gz has been saved\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "Spacing: (0.7421875, 0.7421875, 3.0) -> [1.298828125, 1.298828125, 3.0]\n",
      "Size (448, 448, 153) -> [256. 256. 153.]\n",
      "./sabs_CT_normalizedv2/image_19.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_19.nii.gz has been saved\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "Spacing: (0.8984375, 0.8984375, 3.0) -> [1.572265625, 1.572265625, 3.0]\n",
      "Size (448, 448, 198) -> [256. 256. 198.]\n",
      "./sabs_CT_normalizedv2/image_2.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_2.nii.gz has been saved\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "Spacing: (0.837890625, 0.837890625, 3.0) -> [1.46630859375, 1.46630859375, 3.0]\n",
      "Size (448, 448, 93) -> [256. 256.  93.]\n",
      "./sabs_CT_normalizedv2/image_20.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_20.nii.gz has been saved\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "Spacing: (0.740234375, 0.740234375, 3.0) -> [1.29541015625, 1.29541015625, 3.0]\n",
      "Size (448, 448, 144) -> [256. 256. 144.]\n",
      "./sabs_CT_normalizedv2/image_21.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_21.nii.gz has been saved\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "Spacing: (0.8144530057907104, 0.8144530057907104, 5.0) -> [1.4252927601337433, 1.4252927601337433, 5.0]\n",
      "Size (448, 448, 104) -> [256. 256. 104.]\n",
      "./sabs_CT_normalizedv2/image_22.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_22.nii.gz has been saved\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Label values: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "Spacing: (0.671875, 0.671875, 5.0) -> [1.17578125, 1.17578125, 5.0]\n",
      "Size (448, 448, 98) -> [256. 256.  98.]\n",
      "./sabs_CT_normalizedv2/image_23.nii.gz has been saved\n",
      "./sabs_CT_normalizedv2/label_23.nii.gz has been saved\n",
      "Spacing: (0.9199219942092896, 0.9199219942092896, 5.0) -> [1.6098634898662567, 1.6098634898662567, 5.0]\n",
      "Size (448, 448, 94) -> [256. 256.  94.]\n",
      "Label values: [ 0  1  2  3  5  6  7  8  9 10 11 12 13]\n",
      "Spacing: (0.9199219942092896, 0.9199219942092896, 5.0) -> [1.6098634898662567, 1.6098634898662567, 5.0]\n",
      "Size (448, 448, 94) -> [256. 256.  94.]\n",
      "Spacing: (0.9199219942092896, 0.9199219942092896, 5.0) -> [1.6098634898662567, 1.6098634898662567, 5.0]\n",
      "Size (448, 448, 94) -> [256. 256.  94.]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "OUT_FOLDER = \"./sabs_CT_normalized\"\n",
    "scan_dir = OUT_FOLDER\n",
    "os.makedirs(scan_dir, exist_ok = True)\n",
    "BD_BIAS = 32 # cut irrelavent empty boundary to make roi stands out\n",
    "\n",
    "SPA_FAC = (512 - 2 * BD_BIAS) / 256 # spacing factor\n",
    "\n",
    "for img_fid, seg_fid, pid in zip(imgs, segs, pids):\n",
    "\n",
    "    lb_n = nio.read_nii_bysitk(seg_fid)\n",
    "\n",
    "    img_obj = sitk.ReadImage( img_fid )\n",
    "    seg_obj = sitk.ReadImage( seg_fid )\n",
    "\n",
    "    ## image\n",
    "    array = sitk.GetArrayFromImage(img_obj)\n",
    "    # cropping\n",
    "    array = array[:, BD_BIAS: -BD_BIAS, BD_BIAS: -BD_BIAS]\n",
    "    cropped_img_o = sitk.GetImageFromArray(array)\n",
    "    cropped_img_o = copy_spacing_ori(img_obj, cropped_img_o)\n",
    "\n",
    "    # resampling\n",
    "    img_spa_ori = img_obj.GetSpacing()\n",
    "    res_img_o = resample_by_res(cropped_img_o, [img_spa_ori[0] * SPA_FAC, img_spa_ori[1] * SPA_FAC, img_spa_ori[-1]], interpolator = sitk.sitkLinear,\n",
    "                                    logging = True)\n",
    "\n",
    "    ## label\n",
    "    lb_arr = sitk.GetArrayFromImage(seg_obj)\n",
    "    \n",
    "    # cropping\n",
    "    lb_arr = lb_arr[:,BD_BIAS: -BD_BIAS, BD_BIAS: -BD_BIAS]\n",
    "    cropped_lb_o = sitk.GetImageFromArray(lb_arr)\n",
    "    cropped_lb_o = copy_spacing_ori(seg_obj, cropped_lb_o)\n",
    "\n",
    "    lb_spa_ori = seg_obj.GetSpacing()\n",
    "\n",
    "    # resampling\n",
    "    res_lb_o = resample_lb_by_res(cropped_lb_o,  [lb_spa_ori[0] * SPA_FAC, lb_spa_ori[1] * SPA_FAC, lb_spa_ori[-1] ], interpolator = sitk.sitkLinear,\n",
    "                                  ref_img = res_img_o, logging = True)\n",
    "\n",
    "    \n",
    "    out_img_fid = os.path.join( scan_dir, f'image_{pid}.nii.gz' )\n",
    "    out_lb_fid  = os.path.join( scan_dir, f'label_{pid}.nii.gz' ) \n",
    "    \n",
    "    # then save\n",
    "    sitk.WriteImage(res_img_o, out_img_fid, True) \n",
    "    sitk.WriteImage(res_lb_o, out_lb_fid, True) \n",
    "    print(\"{} has been saved\".format(out_img_fid))\n",
    "    print(\"{} has been saved\".format(out_lb_fid))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
